﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.Models;
using System.Text;
using System.Web.SessionState;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.BLL.Tbl;
using HKEA.CMS.Core.BLL.App;
namespace WebApplication.ServerAshxFile
{
    /// <summary>
    /// M_DeleteContectPerson 的摘要说明
    /// </summary>
    public class M_DeleteContectPerson : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            app_tblCust_ContactPersonBLL app_cust_cpBLL = new app_tblCust_ContactPersonBLL();
            context.Response.ContentType = "text/plain";
            string strRet = string.Empty;
            var customerENT = BasePage.GetInstance().GetCustomerFromSession();
            if (customerENT != null)
            {
                app_tblCust_ContactPerson app_cust_cp = new app_tblCust_ContactPerson();
                string strKeyValue = context.Request.QueryString["UID"];
                if (!string.IsNullOrEmpty(strKeyValue))
                {
                    string[] Info = strKeyValue.Split('-');
                    string From = Info[1];
                    int KeyUID = Convert.ToInt32(Info[0]);
                    if (From == "True")
                    {
                        app_cust_cp.AppCustContactUID = KeyUID;
                        app_cust_cp.CustomerUID = customerENT.CustomerUID;
                        app_cust_cp.Deleted = 0;
                        app_cust_cp = app_cust_cpBLL.GetEntity(app_cust_cp, true);
                        if (app_cust_cp != null)
                        {
                            if (app_cust_cp.CustContactUID > 0)
                            {
                                app_cust_cp.Deleted = 1;
                                if(app_cust_cpBLL.UpdateENT(app_cust_cp) > 0)
                                    strRet = "{\"Issucess\":\"1\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200011") + "\"}";
                                else
                                    strRet = "{\"Issucess\":\"1\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200010") + "\"}";
                            }
                            else
                            {
                                if (app_cust_cpBLL.DeleteENT(app_cust_cp) > 0)
                                    strRet = "{\"Issucess\":\"1\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200011") + "\"}";
                                else
                                { strRet = "{\"Issucess\":\"0\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200010") + "\"}"; }
                            }
                        }
                        else
                        { strRet = "{\"Issucess\":\"0\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200014") + "\"}"; }
                    }
                    else
                    {
                        app_cust_cp.CustContactUID = KeyUID;
                        app_cust_cp.CustomerUID = customerENT.CustomerUID;
                        app_cust_cp = app_cust_cpBLL.GetEntity(app_cust_cp, false);
                        if (app_cust_cp != null)
                        {
                            if (app_cust_cp.Deleted == 1)
                                strRet = "{\"Issucess\":\"0\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200014") + "\"}";
                            else
                            {
                                app_cust_cp.Deleted = 1;
                                if(app_cust_cpBLL.UpdateENT(app_cust_cp) > 0)
                                    strRet = "{\"Issucess\":\"1\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200011") + "\"}";
                                else
                                    strRet = "{\"Issucess\":\"0\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200010") + "\"}";
                            }
                        }
                        else
                        {
                            tblCust_ContactPerson tbl_cust_cp = new tblCust_ContactPerson();
                            tblCust_ContactPersonBLL tbl_custBLL = new tblCust_ContactPersonBLL();
                            tbl_cust_cp.Deleted = 0;
                            tbl_cust_cp.CustContactUID = KeyUID;
                            tbl_cust_cp = tbl_custBLL.GetEntity(tbl_cust_cp);
                            if (tbl_cust_cp != null)
                            {
                                app_cust_cp.ContactPersonUID = tbl_cust_cp.ContactPersonUID;
                                app_cust_cp.CustContactUID = tbl_cust_cp.CustContactUID;
                                app_cust_cp.CustomerUID = Convert.ToInt32(tbl_cust_cp.CustomerUID);
                                app_cust_cp.Deleted = 1;
                                app_cust_cp.Position_Chi = tbl_cust_cp.Position_Chi;
                                app_cust_cp.Position_Eng = tbl_cust_cp.Position_Eng;
                                app_cust_cp.SourceUID = tbl_cust_cp.SourceUID;
                                if (app_cust_cpBLL.InsertENT(app_cust_cp) > 0)
                                    strRet = "{\"Issucess\":\"1\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200011") + "\"}";
                            }
                            else
                            { strRet = "{\"Issucess\":\"0\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200014") + "\"}"; }
                        }
                    }
                }
            }
            else
            { strRet = "{\"Issucess\":\"0\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200016") + "\"}"; }
            context.Response.Write(strRet);
        }

        protected void WriteToLoge(int CPersonUID)
        {
            string name = string.Empty;
            tblHistory entlog = new tblHistory();
            BasePage page = new BasePage();
            tblCustomer Company = tblCustomerBLL.GetInstance().GetEntity(page.GetEnt());
            tblContactPerson entPerson = new tblContactPerson();
            entPerson.ContactPersonUID = CPersonUID;
            entPerson = tblContactPersonBLL.GetInstance().GetEntity(entPerson);
            if (entPerson == null)
            {
                app_tblContactPerson entAppPerson = new app_tblContactPerson();
                entAppPerson.ContactPersonUID = CPersonUID;
                app_tblContactPersonBLL bllCPerson = new app_tblContactPersonBLL();
                entAppPerson = bllCPerson.GetEntity(entAppPerson);
                name = entAppPerson.LastName_Eng + entAppPerson.FirstName_Eng;
            }
            else
            {
                name = entPerson.LastName_Eng + entPerson.FirstName_Eng;
            }
            string LoginName = tblCust_WebLoginBLL.GetInstance().GetENT(page.GetMemberLoginUID()).LoginName.ToString();
            tblUser ent = page.GetManageFromSession();
            entlog.HistoryCatUID = 2;
            entlog.HistoryTypeUID = 6;
            entlog.CustomerUID = Company.CustomerUID;
            entlog.Cust_MemberUID = 0;
            entlog.ContactPersonUID = 0;
            entlog.History_Detail = LoginName + " {" + Company.Name_Eng + "}{ContectPerson}Delete the " + name + " information";
            entlog.LastModifyBy = LoginName;
            int tal = HKEA.CMS.Core.BLL.Tbl.tblHistoryBLL.GetInstance().InsertENT(entlog);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}